#!/usr/bin/python
#coding=utf-8
'''
    4.1. Manually Consuming an Iterator
    4.2. Delegating Iteration
    4.3. Creating New Iteration Patterns with Generators
'''

items = [1, 2, 3]
it = iter(items)
print(next(it))
print(next(it))
print(next(it))

# 4.2. Delegating Iteration
class Node:
    def __init__(self, value):
        self._value = value
        self._children = []

    def __repr__(self):
        return 'Node({!r})'.format(self._value)

    def add_child(self, node):
        self._children.append(node)

    def __iter__(self):
        return iter(self._children)

if __name__ == '__main__':
    root = Node(0)
    child1 = Node(1)
    child2 = Node(2)
    root.add_child(child1)
    root.add_child(child2)
    for ch in root:
        print(ch)

# 4.3. Creating New Iteration Patterns with Generators
def frange(start, stop, increment):
    x = start
    while x < stop:
        yield x
        x += increment